package de.metalcon.gwt.server.modules;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.DynamicRelationshipType;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.ReturnableEvaluator;
import org.neo4j.graphdb.StopEvaluator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.Traverser;
import org.neo4j.graphdb.Traverser.Order;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexManager;

import de.metalcon.gwt.server.ContextListener;
import de.metalcon.gwt.server.db.ANode;
import de.metalcon.gwt.server.db.Relations;
import de.metalcon.gwt.shared.ContentTransmitter;

public class WikiModuleServer extends AModuleServer {

	public WikiModuleServer(HashMap<String, Object> requestVars, ServletContext context) {
		super(requestVars, context);
	}

	@Override
	public ContentTransmitter getTransmitter() {
		ContentTransmitter t = new ContentTransmitter();

		String key = (String) requestVars.get("key");

		for (Map.Entry<String, Object> entry : requestVars.entrySet()) {
			t.setString(entry.getKey(), (String) entry.getValue());

		}

		final GraphDatabaseService db = ContextListener.getNeo4JInstance(context);
		Index<Node> index = db.index().forNodes("type");
		Node article = index.get("key",key).getSingle();

		/**
		 * LETS GO following stuff will work on the data base ( /var/lib/neo4j/wiki
		 * 
		 * DynamicRelationshipType.withName("FOLLOWS") /directed for a link from one article to another
		 * DynamicRelationshipType.withName("ci:"+key)
		 * DynamicRelationshipType.withName("ego:"+key)
		 * 
		 */
		
		DynamicRelationshipType followType = DynamicRelationshipType.withName("FOLLOWS");
		DynamicRelationshipType egoType = DynamicRelationshipType.withName("ego:"+key);
		DynamicRelationshipType ciType = DynamicRelationshipType.withName("ci:"+key);
		
		//followed by
		//ContentTransmitter follower = new ContentTransmitter();
		ContentTransmitter followerList = new ContentTransmitter();
		int i=0;
		for (Relationship rel: article.getRelationships(followType,Direction.INCOMING)){
			Node n = rel.getStartNode();
			ContentTransmitter follower = new ContentTransmitter();
			follower.setString("key", (String)n.getProperty("key"));
			follower.setString("title", (String)n.getProperty("title"));
			followerList.addListElement((String)n.getProperty("key"), follower);
			if (i++>7)break;
		}
		t.addListElement("follower", followerList);
		
		//follows
		ContentTransmitter followsList = new ContentTransmitter();
		i=0;
		for (Relationship rel: article.getRelationships(followType,Direction.OUTGOING)){
			Node n = rel.getEndNode();
			ContentTransmitter follows = new ContentTransmitter();
			follows.setString("key", (String)n.getProperty("key"));
			follows.setString("title", (String)n.getProperty("title"));
			followsList.addListElement((String)n.getProperty("key"), follows);
			if (i++>7)break;
		}

		t.addListElement("follows", followsList);

		i=0;
		int j=0;
		Node n  = article;
		ContentTransmitter stream = new ContentTransmitter();
		String s="";
		while (i<30){
			n = getNextNode(n,egoType);
			if (n==null)break;
			ContentTransmitter entry = new ContentTransmitter();
			Node ci = getNextNode(n,DynamicRelationshipType.withName("ci:"+(String)n.getProperty("key")));
			if (ci==null)continue;
			i++;
			String ts= (String)ci.getProperty("timestamp");		
			/*if (ts==null)continue;
			Timestamp timestamp = new Timestamp((long)ts);
			Date date = new Date(timestamp.getTime());*/
/*			entry.setString("timestamp", ts);
			entry.setString("key", (String)n.getProperty("key"));
			entry.setString("title", (String)n.getProperty("title"));
			stream.addListElement(""+j++, entry);
			j++;*/
			s = s+ ts + "<br>" + "<a href='#Wiki_"+(String)n.getProperty("key")+"'>"+(String)n.getProperty("title")+"</a><br><br>";
		}
		t.setString("numnews", j+"");
		//t.addListElement("stream", stream);
		t.setString("stream", s);
		t.setString("title", (String)article.getProperty("title"));
		return t;
	}

	private Node traverseNode(Node n, DynamicRelationshipType dyn, Direction direction){
		Node res=null;
		for (Relationship rel : n.getRelationships(dyn,direction)){
			res = (Node) rel.getOtherNode(n);
		}
		return res;		
	}
	
	private Node getNextNode(Node n, DynamicRelationshipType dyn){
		return traverseNode(n,dyn,Direction.OUTGOING);
	}
	
}
